package com.sidianzhong.sdz.service.impl;

import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class WebInspectionitemServiceImpl implements WebInspectionitemService{
    protected static final Logger LOG = LoggerFactory.getLogger(WebInspectionitemServiceImpl.class);

    // 将所有的modelMapper注入
    @Autowired
    private WebInspectionitemMapper webInspectionitemMapper;


    @Override
    public WebInspectionitem create(WebInspectionitem item) {

         webInspectionitemMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return webInspectionitemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(WebInspectionitem item) {

        return webInspectionitemMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public WebInspectionitem get(Integer id) {
        return webInspectionitemMapper.selectByPrimaryKey(id);
    }
    @Override
     public PageInfo<WebInspectionitem> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,WebInspectionitem item){

        WebInspectionitemExample  example = new WebInspectionitemExample();
        WebInspectionitemExample.Criteria criteria = example.createCriteria();
        if(item.getEquipmenttypekey() != null ){
            criteria.andEquipmenttypekeyEqualTo(item.getEquipmenttypekey());
        }
        if(item.getEquipmentkey() != null ){
            criteria.andEquipmentkeyEqualTo(item.getEquipmentkey());
        }
        if(item.getItemname() != null ){
            criteria.andItemnameEqualTo(item.getItemname());
        }
        if(item.getItemtype() != null ){
            criteria.andItemtypeEqualTo(item.getItemtype());
        }
        if(item.getItemunits() != null ){
            criteria.andItemunitsEqualTo(item.getItemunits());
        }

        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List< WebInspectionitem> list = this.webInspectionitemMapper.selectByExample(example);
        PageInfo result = new PageInfo(list);

        return result;

     }

    @Override
    public List<WebInspectionitem> getList(Integer pageNum, Integer pageSize, String sortItem, String sortOrder, WebInspectionitem item) {

        WebInspectionitemExample  example = new WebInspectionitemExample();
        WebInspectionitemExample.Criteria criteria = example.createCriteria();
        if(item.getEquipmenttypekey() != null ){
            criteria.andEquipmenttypekeyEqualTo(item.getEquipmenttypekey());
        }
        if(item.getEquipmentkey() != null ){
            criteria.andEquipmentkeyEqualTo(item.getEquipmentkey());
        }
        if(item.getItemname() != null ){
            criteria.andItemnameEqualTo(item.getItemname());
        }
        if(item.getItemtype() != null ){
            criteria.andItemtypeEqualTo(item.getItemtype());
        }
        if(item.getItemunits() != null ){
            criteria.andItemunitsEqualTo(item.getItemunits());
        }

        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List< WebInspectionitem> list = this.webInspectionitemMapper.selectByExample(example);
        return list;
    }

}
